package com.example.myroomdemo.ui.home

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myroomdemo.MyApp
import com.example.myroomdemo.R
import com.example.myroomdemo.adapter.Adapter_Recharge
import com.example.myroomdemo.beans.Users
import com.example.myroomdemo.beans.roomSql.UserDao
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
import kotlinx.android.synthetic.main.fragment_home.*
import kotlin.random.Random

/**
 * 转化失败了？
 * 存取失败！
 */
class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel
    private var userDao: UserDao? = null
    private var ctx: Context? = null
    private var list_data = mutableListOf<Users>()

    private val mAdapter by lazy {
        this?.let {
            Adapter_Recharge(
                ctx!!,
                R.layout.lay_item_room,
                list_data
            )
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        ctx = context
        homeViewModel =
            ViewModelProviders.of(this).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_home, container, false)
        /*    val textView: TextView = root.findViewById(R.id.text_home)
            homeViewModel.text.observe(viewLifecycleOwner, Observer {
                textView.text = it
            })*/

        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        userDao = MyApp.mInstance?.getAppDB()?.UserDao()
        initView()
    }

    private fun initView() {
        rlv_info?.apply {
            var wllm = LinearLayoutManager(ctx)
            layoutManager = wllm
            itemAnimator = DefaultItemAnimator()
            mAdapter.setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener {
                override fun onItemLongClick(
                    p0: View?,
                    p1: RecyclerView.ViewHolder?,
                    p2: Int
                ): Boolean {

                    var changeUser = list_data.get(p2)
                    changeUser.strName = "李福昌"
                    var listUsers = mutableListOf<Users>()
                    listUsers.add(changeUser)
                    userDao?.update(listUsers)
                    getAllData()
                    return false
                }

                override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                }

            })
            adapter = mAdapter
        }
        btn_01.setOnClickListener {
//增一个
            addOne()
        }
        btn_011.setOnClickListener {
//增多个
            addMore()
        }
        btn_02.setOnClickListener {
//删
            DelAll()
        }
        btn_03.setOnClickListener {
//查一个
            getOne()
        }
        btn_031.setOnClickListener {
//查多个
            getAllData()
        }

    }

    /**
     * 获取一个
     */
    private fun getOne() {


    }

    /**
     * 删除全部
     */
    private fun DelAll() {
        userDao?.deleteALl()
        getAllData()
    }

    /**
     * 添加多个
     */
    private fun addMore() {
        userDao?.insertAll(creatMoreUser())
        getAllData()

    }


    /**
     * 添加一个
     */
    private fun addOne() {
//        var listData =
        userDao?.insertOne(creatOneUser())
        getAllData()
    }


    /**
     * 获取多个
     */
    private fun getAllData() {
        list_data.clear()
        var listUsers = userDao?.getAllData()
        if (listUsers != null && listUsers.size > 0) {
            list_data.addAll(listUsers as MutableList<Users>)
            mAdapter.notifyDataSetChanged()
        } else {
            list_data.clear()
            mAdapter.notifyDataSetChanged()
        }
    }

    /**
     * 增加一个
     */
    private fun creatOneUser(): Users? {
        var user = Users()
        user.strName = "李昕柔"
        user.age = 18 + Random.nextInt(7)
        var sex = Random.nextInt(1) + 1
        user.sex = sex
        user.strNote = if (sex == 1) "小狼狗" else "美少女"
        return user
    }

    /**
     * 增加多个
     */
    private fun creatMoreUser(): MutableList<Users>? {
        var animes = mutableListOf<Users>()
        for (index in 1..Random.nextInt(5)) {
            var user = Users()
            user.strName = "李昕柔"
            user.age = 18 + Random.nextInt(7)
            var sex = Random.nextInt(1) + 1
            user.sex = sex
            user.strNote = if (sex == 1) "小狼狗" else "美少女"
            user.strHobby = "画画"
            user.strWeight = "画画"
            user.height = 180
            var ads = Users.Address()
            ads.adddressName = "郑州市登封市$index"
            ads.code = index * 100 + Random.nextInt(100)
            user.adsInfo = ads
            var listbooks = mutableListOf<Users.Books>()
            for (indexs in 0..Random.nextInt(5)) {
                var books = Users.Books()
                books.bookName = "三字经"
                books.code = index * 10 + indexs
                listbooks.add(books)
            }
            user.listBooks = listbooks
            animes.add(user)
        }
        return animes
    }

}